package com.tuojiahr.action;

import java.io.IOException;

import com.opensymphony.xwork2.Preparable;
import com.tuojiahr.common.BaseActionCommon;
import com.tuojiahr.domain.TuoJiaHrSpecialColumn;
import com.tuojiahr.domain.TuoJiaHrUser;
import com.tuojiahr.global.Constants;
import com.tuojiahr.pagemodel.PageModel;
import com.tuojiahr.service.SpecialColumnService;
import com.tuojiahr.tools.Pager;
import com.tuojiahr.tools.StringUtil;

/**
 * 说明：
 *
 * @author wang.haibo
 * @date 2012-11-10 下午4:21:14
 */

public class SpecialColumnAction extends BaseActionCommon implements Preparable {

	private static final long serialVersionUID = 1L;
	private SpecialColumnService specialColumnService;
	
	private PageModel pageModel;
	private TuoJiaHrSpecialColumn specialColumn;
	private Pager pager;
	
	private String keyWord;
	private String url = "adminmgr/sc_specialColumnList";
	private String message = "出错啦！！！";
	
	private TuoJiaHrUser user = null;
	
	@Override
	public void prepare() throws Exception {
		user = getCurrUser();
	}
	
	/**
	 * 分页获取普通栏目，支持根据名称模糊查询
	 * @return
	 */
	public String specialColumnList() {
		if(null != user) {
			try {
				pageModel = specialColumnService.getSpecialColumn(keyWord);
				return "special_view";
			} catch (Exception e) {
				Constants.ExceptionLog.error("##SpecialColumnAction---specialColumnList方法查询出错:", e);
			}
		}
		return "msg";
	}
	
	/**
	 * 根据id查看一条记录
	 * @return
	 */
	public String openOneView() {
		if(null != user) { 
			String specialId = request.getParameter("specialId");
			if(StringUtil.isNotBlank(specialId)) {
				try {
					specialColumn = specialColumnService.getOrdinaryColumnById(Long.valueOf(specialId));
					return "one_special_view";
				} catch (Exception e) {
					Constants.ExceptionLog.error("##SpecialColumnAction---openOneView方法查询出错:", e);
				}
			}
		}
		return "msg";
	}
	
	/**
	 * 打开添加或更新窗口
	 * @return
	 */
	public String openAddOrUpdate() {
		if(null != user) {
			String specialId = request.getParameter("specialId");
			if(StringUtil.isNotBlank(specialId)) {
				specialColumn = specialColumnService.getOrdinaryColumnById(Long.valueOf(specialId));
				url = "adminmgr/sc_updateSpecialColumn";
			} else {
				url = "adminmgr/sc_addSpecialColumn";
			}
			return "add_update_special";
		}
		return "msg";
	}
	
	/**
	 * 添加
	 */
	public String addSpecialColumn()  {
		if(null != user) {
			specialColumn.setOperator(user);
			try {
				specialColumnService.addSpecialColumn(specialColumn);
				return "action";
			} catch (Exception e) {
				Constants.ExceptionLog.error("##SpecialColumnAction---addSpecialColumn方法添加出错:", e);
			}
		} 
		return "msg";
	}
	
	/**
	 * 编辑
	 */
	public String updateSpecialColumn() {
		if(null != user) {
			if(null != specialColumn && 0 != specialColumn.getId()) {
				specialColumn.setOperator(user);
				try {
					specialColumnService.updateSpecialColumn(specialColumn);
					return "action";
				} catch (Exception e) {
					Constants.ExceptionLog.error("##SpecialColumnAction---addSpecialColumn方法更新出错:", e);
				}
			}
		}
		return "msg";
	}

	/**
	 * 删除
	 * @throws IOException 
	 */
	public void delSpecialById() throws IOException {
		String ids = request.getParameter("ids");
		String result = "fault";
		if(StringUtil.isNotBlank(ids)) {
			try {
				result = specialColumnService.deleteSpecialColumn(Long.valueOf(ids));
			} catch (Exception e) {
				result = "error";
				Constants.ExceptionLog.error("##SpecialColumnAction---delSpecialById方法删除出错:", e);
			}
		}
		response.getWriter().print(result);
		response.getWriter().close();
	}
	
	public PageModel getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public TuoJiaHrSpecialColumn getSpecialColumn() {
		return specialColumn;
	}

	public void setSpecialColumn(TuoJiaHrSpecialColumn specialColumn) {
		this.specialColumn = specialColumn;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setSpecialColumnService(SpecialColumnService specialColumnService) {
		this.specialColumnService = specialColumnService;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
